﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "GcmpUiQtImpl.h"
#include "IDockManager.h"
#include <Qt>
#include <map>

class QMainWindow;
class QDockWidget;

namespace gcmp
{
    class IApplicationWindow;
    class IPropertyWidget;

    //////////////////////////////////////////////////////////////////////////
    //浮动面板管理器：负责管理各种浮动面板
    //////////////////////////////////////////////////////////////////////////
    class GCMP_UI_QT_IMPL_EXPORT DockManagerImpl : public IDockManager
    {
    private:
        struct DockInfo
        {
            std::wstring name;
            DockArea area;
            DockOrientation orientation;
        };

    public:
        explicit DockManagerImpl(IApplicationWindow* mainWindow);
        ~DockManagerImpl();

    public:
        virtual OwnerPtr<IDock> CreateDock(const std::wstring& name, const std::wstring& title) const override;

        virtual void AddDock(OwnerPtr<IDock> opDock, DockArea area, DockOrientation orientation) override;

        virtual void DeleteDock(const std::wstring& name) override;

        virtual IDock* GetDock(const std::wstring& name) override;

        virtual void ResizeDocks(const std::vector<IDock*>& docks, const std::vector<int>& sizes, DockOrientation orientation) override;
        
        virtual void SplitDock(IDock *pFirstDock, IDock *pSecondDock, DockOrientation orientation) override;

        virtual std::vector<IDock*> TabifiedDocks(IDock* pDock) const override;

        virtual void TabifyDock(IDock* pFirstDock, IDock* pSecondDock) override;

        // 显示/隐藏面板。
        virtual void ShowAllDockPane() override;
        virtual void HideAllDockPane() override;

        virtual bool IsDockable(const std::wstring & name) const override;
        virtual bool SetIsDockable(const std::wstring & name, bool isDockable) override;

        virtual bool RemoveDockTitleBar(const std::wstring & name) override;

        virtual void UpdateAllDockStatus() override;

    public:
        static IPropertyWidget* GetPropertyWidget();

    private:
        QMainWindow* GetQMainWindow() const;
        QDockWidget* GetQDockWidget(IDock* pDock) const;
        Qt::Orientation GetQtOrientation(DockOrientation orientation) const;
        Qt::DockWidgetArea GetQtDockArea(DockArea area) const;

    private:
        IApplicationWindow* m_pIMainWindow;

        std::map<std::wstring, IDock*> m_nameToIDockMap;
        std::map<QDockWidget*, IDock*> m_qDockWidgetToIDockMap;
    };
}
